<?php
/**
 * 应用自定义视图模板类
 * 
 * @version $Id: AppView.php 481 2012-11-04 02:19:24Z linsir123 $
 */
class AppView
{
	/**
	 * @var 相对路径
	 */
	static $domain;
	
	/**
	 * @var Css集合
	 */
	static $css;
	
	/**
	 * @var Javascript集合
	 */
	static $js;
	
	/**
	 * 初始化
	 */
	static function init()
	{
		static $__init = false;
		if ($__init) {
			return ;
		}
		$__init = true;
		
		///
		$view = App::getView();
		$engine = & $view->engine;
		
		self::$css = array('themes/css/layout%d.css', 'themes/01/_global.css');
		self::$js = array('_e35go/global.js');
		self::$domain = App::getRequest()->domain;
		
		/// 过滤器
		//$engine->registerFilter('pre', array('AppView', 'prefilter'));
		
		/// 动态模块
		$engine->registerPlugin('block', 'dynamic', 		array('AppView', 'dynamic'), false);
		
		/// 函数
		$engine->registerPlugin('function', 'JS', 			array('AppView', 'sDomJs'));
		$engine->registerPlugin('function', 'CSS', 			array('AppView', 'sDomCss'));
		$engine->registerPlugin('function', 'shopScore', 	array('AppView', 'sShopScore'));
		$engine->registerPlugin('function', 'itemInfo', 	array('AppView', 'sItemInfo'));
		$engine->registerPlugin('function', 'itemPrice', 	array('AppView', 'sItemPrice'));
		
		/* 链接相关 */
		$engine->registerPlugin('function', 'urlItem', 		array('Route', 'urlItem'));
		$engine->registerPlugin('function', 'urlItemJump', 	array('Route', 'urlItemJump'));
		$engine->registerPlugin('function', 'urlShop', 		array('Route', 'urlShop'));
		$engine->registerPlugin('function', 'urlShopJump', 	array('Route', 'urlShopJump'));
		$engine->registerPlugin('function', 'urlClass', 	array('Route', 'urlClass'));
		$engine->registerPlugin('function', 'urlPhoto',		array('Route', 'urlPhoto'));
		
		/* 导航相关 */
		$engine->registerPlugin('function', 'navTop', 			array('Nav', 'top'));
		$engine->registerPlugin('function', 'navHeader', 		array('Nav', 'header'));
		$engine->registerPlugin('function', 'navSub', 			array('Nav', 'sub'));
		$engine->registerPlugin('function', 'navTmallBrandSub',	array('Nav', 'tmallBrandSub'));
		
		/**/
		$engine->registerPlugin('function', 'homeUserPannel',	array('AppView', 'homeUserPannel'));
	}
	
	/**
	 * 压缩html
	 */
	static function prefilter($content, & $smarty)
	{
		// 保护smarty标签将<-- {xxx} -->替换成 {xxx}
		$content = preg_replace('/<!--[^>|\n]*?({.+?})[^<|{|\n]*?-->/', '\1', $content);
		// 去掉html注释
		$content = preg_replace('/<!--.*?-->/', '', $content);
		// 压缩html代码,去掉行首尾空格及换行
		return preg_replace("/\s*[\n\r]+\s*/", '', $content);
	}
	
	/**
	 * 动态内容
	 */
	static function dynamic($param, $content)
	{
		return $content;
	}
	
	/**
	 * 添加Css
	 */
	static function addCss($css)
	{
		$args = func_get_args();
		
		///
		foreach ($args as $v) 
			array_push(self::$css, $v);
	}
	
	/**
	 * 添加Javascript
	 */
	static function addJs($js)
	{
		$args = func_get_args();
		
		///
		foreach ($args as $v) 
			array_push(self::$js, $v);
	}
	
	/**
	 * 店铺积分
	 */
	static function sShopScore($params = null)
	{
		$score = floatval($params['score']);
		$score = intval($score*10);
		for ($i=50; $i>0; $i-=5) {
			if ($score >= $i) {
				$score = $i;
				break;
			}
		}
		
		return '<img src="' . self::$domain.'images/score/'.$score.'.gif" width="60" height="12" />&nbsp;'.$params['score'];
	}
	
	/**
	 * 商品信息明细
	 * // 区分IE6
	 */
	static function sItemInfo($params = null)
	{
		$pattern = "/(?<=href=)([^\>]*)(?=\>)/i";
		///
		return preg_replace($pattern, "#", $params['desc']);
	}
	
	/**
	 * 商品返利
	 */
	static function sItemPrice($params = null)
	{
		static $rate;
		if ( ! isset($rate)) 
			$rate = App::O('taobaoke.rate');
			
		///
		$price = floatval($params['price']);
		$len = intval($params['len']);
		if (empty($len)) 
			$len = 2;
			
		///
		return sprintf("￥%.{$len}f", $rate*$price);
	}
	
	/**
	 * Css
	 */
	static function sDomCss($params = null)
	{
		$layout = intval($params['layout']);
		if (empty($layout)) 
			$layout = 2;
			
		self::$css[0] = sprintf(self::$css[0], $layout);
		
		///
		if (strlen(self::$domain)==1) 
			$b = '';
		else {
			$b = substr(self::$domain, 1);
			$b = substr($b, 0, strlen($b)-1);
			$b = 'b='.$b.'&amp;';
		}
		$content = '<link type="text/css" rel="stylesheet" href="';
		$content .= self::$domain.'min/'.$b.'f='.implode(',', self::$css);
		$content .= '?v=20120902" />';
		return $content;
	}
	
	/**
	 * Javascript
	 */
	static function sDomJs($params = null)
	{
		/*$content = '<script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/1.6.2/jquery.min.js"></script>';
		$content .= "\r\n";*/
		$content .= '<script type="text/javascript">if(!window.jQuery){document.write(\'<script type="text/javascript" src="'.self::$domain.'javascript/jquery-1.6.2.min.js"><\/script>\');}</script>';
		$content .= "\r\n";
		
		///
		$content .= '<script type="text/javascript" src="';
		$content .= self::$domain.'min/b='.substr(self::$domain, 1).'javascript&amp;f='.implode(',', self::$js);
		$content .= '"></script>';
		///
		
		return $content;
	}
	
	/**
	 * 首页用户面板
	 */
	static function homeUserPannel($params = null)
	{
		$user = M::user()->data;
		$exchange = M::exchange()->getInfo();
		$account = M::account()->getInfo();
		$order = M::order()->getInfo(false);
		
		///
		$urlMain = lpUrl::__('user/main');
		$urlLogout = lpUrl::__('user/site/logout');
		$userName = $user['username'];
		$userTime = $user['login_time'];
		$userTimes = $user['login_times'];
		$orderCount = $order['count'];
		$orderCommission = sprintf("￥%.2f", $order['commission']);
		$exchangeTotal = sprintf("￥%.2f", $exchange['total']);
		
		///
return <<< DOM
<p>您好，<label>{$userName}</label></p>
<p>
	欢迎您回来！
	<a href="{$urlMain}">[会员中心]</a>
	<a href="{$urlLogout}">[退出]</a>
</p>
<p class="login_times">您上次登录时间：</p>
<p class="m">{$userTime}</p>
<p class="m">总共登录过&nbsp;<b>{$userTimes}</b>&nbsp;次</p>
<div class="order">
	<p>完成订单：<b>{$orderCount}</b></p>
	<p>总返现金额：<b>{$orderCommission}</b></p>
	<p>已提现金额：<b>{$exchangeTotal}</b></p>
</div>
DOM;
	}
}
